#include "typeconver.h"
#include <QString>
/**
 * @brief 判断字符串是否为整数（仅判断格式，不考虑范围）
 * @param char*
 * @return
 */
bool isInt(const char* str)
{
    bool isNum = false;

    int index = 0;
    for (; *str != '\0'; str++, index++){
        switch (*str)
        {
            case '0':case'1':case'2':case'3':case'4':case'5':
            case'6':case'7':case'8':case'9':
                isNum = true;
                break;
            case '-':case '+':
                if (index != 0){
                    return false;
                }
                break;
            default:
                return false;
        }
    }
    if(!isNum){return false;}

    return true;
}


/**
 * @brief 判断字符串是否为浮点数（仅判断格式，不考虑范围）
 * @param char *
 * @return
 */
bool isFloat(const char * str)
{
    bool isE = false,
    isPoint = false,
    numBefore = false,
    numBehind = false;

    int index = 0;
    for (; *str != '\0'; str++, index++)
    {
        switch (*str)
        {
        case '0':case'1':case'2':case'3':case'4':case'5':
        case'6':case'7':case'8':case'9':
            if (isE){
                numBehind = true;
            }
            else{
                numBefore = true;
            }
            break;
        case '+':case '-':
            if (index != 0){
                return false;
            }
            break;
        case 'e':case 'E':
            if (isE || !numBefore){
                return false;
            }else{
                isPoint = true;
                index = -1;
                isE = true;
            }
            break;
        case '.':
            if (isPoint){
                return false;
            }else{
                isPoint = true;
            }
            break;
        default:
            return false;
        }
    }

    if (!numBefore){
        return false;
    }else if (isE && !numBehind){
        return false;
    }

    return true;
}



/**
 * @brief 将字符型进制转化为16进制
 * @param str
 * @return QByteArray
 */
QByteArray QString2Hex(QString str)
{
    QByteArray senddata;
    int hexdata,lowhexdata;
    int hexdatalen = 0;
    int len = str.length();
    senddata.resize(len/2);
    char lstr,hstr;
    for(int i=0; i<len; ){
        hstr=str[i].toLatin1();   //字符型
        if(hstr == ' '){
            i++;
            continue;
        }
        i++;
        if(i >= len)
            break;
        lstr = str[i].toLatin1();
        hexdata = ConvertHexChar(hstr);
        lowhexdata = ConvertHexChar(lstr);
        if((hexdata == 16) || (lowhexdata == 16))
            break;
        else
            hexdata = hexdata*16+lowhexdata;
        i++;
        senddata[hexdatalen] = (char)hexdata;
        hexdatalen++;
    }
    senddata.resize(hexdatalen);
    return senddata;
}

/**
 * @brief 将单个字符串转换为hex
 * @param ch
 * @return char
 */
char ConvertHexChar(char ch)
{
    if((ch >= '0') && (ch <= '9'))
        return ch-0x30;
    else if((ch >= 'A') && (ch <= 'F'))
        return ch-'A'+10;
    else if((ch >= 'a') && (ch <= 'f'))
        return ch-'a'+10;
    else return (-1);
}
